package com.xzcedu.homework;

import java.io.IOException;
import java.net.*;

/**
 * @author 徐州城
 * @version 1.0
 * UDP接收端
 */
public class Homework02ReceiverA {
    public static void main(String[] args) throws IOException {

        //创建socket
        DatagramSocket socket = new DatagramSocket(8888);

        byte[] buf = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);

        System.out.println("接收端A 端口8888 等待接收数据....");
        socket.receive(packet);

        int length = packet.getLength();//得到实际字节的长度
        byte[] data = packet.getData();
        String s = new String(data, 0, length);
        System.out.println(s);

        String an = "";
        if (s.equals("四大名著是哪些")) {
            an = "<<红楼梦>> <<三国演义>> <<西游记>> <<水浒传>>";
        } else {
            an = "want?";
        }
        packet = new DatagramPacket(an.getBytes(), 0,an.length(),InetAddress.getByName("192.168.1.1"),9998);
        socket.send(packet);
        //退出流
        socket.close();

        System.out.println("接收端退出...");
    }
}
